/*
 * MailBox.h
 *
 *  Created on: 2013. 10. 9.
 *      Author: mega@gamevil.com
 */

#ifndef MAILBOX_H_
#define MAILBOX_H_

#include <Network/Static.h>
#include <Toolkit/Str.h>
#include <map>

#include "Item.h"

namespace ProjectZ {
class User;
} /*ProjectZ*/

namespace ProjectZ { namespace NLogic {

class MailBox
{
public:
	class Mail
	{
	public:
		struct Data
		{
			int send_user_seq;
			int recv_user_seq;
			Toolkit::Str<80> send_user_nickname;
			Toolkit::Str<1024> send_user_imageurl;

			int mail_status;
			int gift_status;		// 0:받을 선물 없음, 1:골드 존재, 2:젠 존재, 4:아이템 존재, 7:모두존재
			int gift_vcindex;		// 젠 선물하기인 경우 vc index

			int zen;
			int gold;

			Toolkit::Str<180> message;

			time_t send_date;

			struct NLogic::Item::Data item;

			Data()
			{
				send_user_seq = 0;
				recv_user_seq = 0;

				mail_status = 0;
				gift_status = 0;
				gift_vcindex = -1;

				zen = 0;
				gold = 0;

				message.Clear();
				send_user_nickname.Clear();
				send_user_imageurl.Clear();

				time(&send_date);

				item.Clear();
			}

			void Init()
			{
				send_user_seq = 0;
				recv_user_seq = 0;

				mail_status = 0;
				gift_status = 0;
				gift_vcindex = -1;

				zen = 0;
				gold = 0;

				message.Clear();
				send_user_nickname.Clear();
				send_user_imageurl.Clear();

				time(&send_date);

				item.Clear();
			}
		};

	public:
		Mail() : _user(NULL), _mailSeq(0) {};
		virtual ~Mail() {};

	public:
		void 			Initialize() { _user = NULL; _mailSeq = 0; };
		void 			Finalize() { };

	public:
		void		Open_Normal(Data & data, User * pUser);
		void		Open_FromDatabase(uint64_t mailSeq, Data & data, User * pUser);
		Data &		GetData();

		bool		SelectMailShardType(int & shardType);
		bool		InsertDatabaseMail();
		bool		UpdateReadStatusDatabaseMail();
		bool		UpdateDeleteStatusDatabaseMail();
		bool		UpdateGiftStatusDatabaseMail();

		uint64_t	GetMailSeq() { return _mailSeq; }

		void		WriteToItemPacket(::Network::Packet * pPacket);

	private:
		Data		_data;
		User *		_user;
		uint64_t	_mailSeq;
	};


public:
	MailBox();
	virtual ~MailBox();

	void 		Initialize();
	void 		Finalize();

	typedef std::map<uint64_t, NLogic::MailBox::Mail *> MAILTREE;

public:
	bool						AddMailData(NLogic::MailBox::Mail * pMail);
	void						DelMailData(uint64_t mailSeq);
	MAILTREE &					GetMailTree() { return clsMailTree; }
	NLogic::MailBox::Mail *		GetMailData(uint64_t mailSeq);

private:
	MAILTREE	clsMailTree;
};

} /*NLogic*/ } /* namespace ProjectZ */

#endif /* MAILBOX_H_ */
